{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "London Tube Lines\n",
    "=================\n",
    "This example shows the London tube lines against the background of the\n",
    "borough boundaries. It is based on the vega-lite example at\n",
    "https://vega.github.io/vega-lite/examples/geo_layer_line_london.html.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# category: geographic\n",
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from vega_datasets import data\n",
    "\n",
    "boroughs = alt.topo_feature(data.londonBoroughs.url, 'boroughs')\n",
    "tubelines = alt.topo_feature(data.londonTubeLines.url, 'line')\n",
    "centroids = data.londonCentroids.url\n",
    "\n",
    "background = alt.Chart(boroughs).mark_geoshape(\n",
    "    stroke='white',\n",
    "    strokeWidth=2\n",
    ").encode(\n",
    "    color=alt.value('#eee'),\n",
    ").properties(\n",
    "    width=700,\n",
    "    height=500\n",
    ")\n",
    "\n",
    "labels = alt.Chart(centroids).mark_text().encode(\n",
    "    longitude='cx:Q',\n",
    "    latitude='cy:Q',\n",
    "    text='bLabel:N',\n",
    "    size=alt.value(8),\n",
    "    opacity=alt.value(0.6)\n",
    ").transform_calculate(\n",
    "    \"bLabel\", \"indexof (datum.name,' ') > 0  ? substring(datum.name,0,indexof(datum.name, ' ')) : datum.name\"\n",
    ")\n",
    "\n",
    "line_scale = alt.Scale(domain=[\"Bakerloo\", \"Central\", \"Circle\", \"District\", \"DLR\",\n",
    "                               \"Hammersmith & City\", \"Jubilee\", \"Metropolitan\", \"Northern\",\n",
    "                               \"Piccadilly\", \"Victoria\", \"Waterloo & City\" ],\n",
    "                       range=[\"rgb(137,78,36)\", \"rgb(220,36,30)\", \"rgb(255,206,0)\",\n",
    "                              \"rgb(1,114,41)\", \"rgb(0,175,173)\", \"rgb(215,153,175)\",\n",
    "                              \"rgb(106,114,120)\", \"rgb(114,17,84)\", \"rgb(0,0,0)\",\n",
    "                              \"rgb(0,24,168)\", \"rgb(0,160,226)\", \"rgb(106,187,170)\"])\n",
    "\n",
    "lines = alt.Chart(tubelines).mark_geoshape(\n",
    "    filled=False,\n",
    "    strokeWidth=2\n",
    ").encode(\n",
    "    alt.Color(\n",
    "        'id:N',\n",
    "        legend=alt.Legend(\n",
    "            title=None,\n",
    "            orient='bottom-right',\n",
    "            offset=0\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "background + labels + lines\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
